Attribute VB_Name = "modODBCTextFile"
Option Explicit
'

'---------------------------------------------------------------------------------------
' Procedure : ArrayCount
' DateTime  : 02/12/2007
' Purpose   : Function returns the element count of an array
'---------------------------------------------------------------------------------------
'
Public Function ArrayCount(ByRef vntArray As Variant, Optional ByVal intType As Integer = 0) As Integer
 
    ' Set in-line error handling
 
    On Error Resume Next
 
    ' Set return value
 
    ArrayCount = -1
 
    Select Case intType
        Case 0
            ArrayCount = UBound(vntArray)             'Normal
        Case 1
            ArrayCount = UBound(vntArray) + 1         'Delim
        Case 2
            ArrayCount = UBound(vntArray, 2) + 1      'Fixed
    End Select
 
 
    Err.Clear
 
End Function
'


'---------------------------------------------------------------------------------------
' Procedure : RecordImport
' DateTime  : 02/12/2007
' Purpose   : Get import data from Delimited File
'---------------------------------------------------------------------------------------
'
Public Function RecordImport(ByVal strIMPORT_FILE_PATH As String, ByVal strFilename As String, Optional strDateCreated As String, Optional strSeparator As String = "|") As String()
 
    On Error GoTo RecordImport_Error
 
    ' Declare variables for procedure
    Dim a As Long
    Dim b As Long
    Dim strLine As String
    Dim strRecords() As String
    Dim strRecord() As String
    Dim strKeyVal As String
 
    ' Open file
    strKeyVal = UCase(strFilename)
    strFilename = Replace(strFilename, " ", "")
    strFilename = UCase(strFilename)
    Open strIMPORT_FILE_PATH & strFilename For Input Access Read Shared As #1
    
    Do Until EOF(1)
        ' Retrieve record from file
        Line Input #1, strLine
        If Trim(strLine) <> "" Then
          strRecord = SplitLine(strLine, strSeparator)
          If UBound(strRecord) >= 0 Then
              If strKeyVal = strRecord(0) Then
                  ReDim Preserve strRecords(16, a) As String
                    ' Set Record
                    For b = 0 To UBound(strRecord)
                        If b > 16 Then Exit For
                        strRecords(b, a) = strRecord(b)
                    Next b
                    a = a + 1
                End If
            End If
         
        End If
    Loop
 
    RecordImport = strRecords
 
Release:
    Close #1
    Erase strRecords
    Erase strRecord
 
    Exit Function
 
RecordImport_Error:
 
    MsgBox Err.Description & " (RecordImport)", vbSystemModal
 
    Resume Release
 
End Function
'

'---------------------------------------------------------------------------------------
' Procedure : RecordImport2
' DateTime  : 02/12/2007
' Purpose   : Get import data from Delimited File
'
' 2010-05-26, JFW, Return all records from file, i.e. no primary key.
'
'---------------------------------------------------------------------------------------
'
Public Function RecordImport2(ByVal strIMPORT_FILE_PATH As String, ByVal strFilename As String, Optional strDateCreated As String, Optional strSeparator As String = "|") As String()
 
    On Error GoTo RecordImport_Error
 
    ' Declare variables for procedure
    Dim a As Long
    Dim b As Long
    Dim strLine As String
    Dim strRecords() As String
    Dim strRecord() As String
    Dim strKeyVal As String
 
    ' Open file
    strKeyVal = UCase(strFilename)
    strFilename = Replace(strFilename, " ", "")
    strFilename = UCase(strFilename)
    Open strIMPORT_FILE_PATH & strFilename For Input Access Read Shared As #1
    
    Do Until EOF(1)
        ' Retrieve record from file
        Line Input #1, strLine
        If Trim(strLine) <> "" Then
          strRecord = SplitLine(strLine, strSeparator)
          If UBound(strRecord) >= 0 Then
              ReDim Preserve strRecords(16, a) As String
                ' Set Record
                For b = 0 To UBound(strRecord)
                    If b > 16 Then Exit For
                    strRecords(b, a) = strRecord(b)
                Next b
                a = a + 1
            End If
        End If
    Loop
 
    RecordImport2 = strRecords
 
Release:
    Close #1
    Erase strRecords
    Erase strRecord
 
    Exit Function
 
RecordImport_Error:
 
    MsgBox Err.Description & " (RecordImport2)", vbSystemModal
 
    Resume Release
 
End Function
'

'---------------------------------------------------------------------------------------
' Procedure : SplitLine
' DateTime  : 02/12/2007
' Purpose   : Parses out a delimited string and returns values in an array
'---------------------------------------------------------------------------------------
'
Public Function SplitLine(strLine As String, Optional strDelimiter As String = "|") As String()
 
    On Error GoTo SplitLine_Error
 
    Dim a As Long
    Dim b As Long
    Dim strCharacter As String
    Dim blnInQualifier As Boolean
    Dim strHolder() As String
 
    ' Search line by character to determine where the delimiter is
    'and if the delimiter is inside a qualifier, then assign
    'the field to the array
    For a = 1 To Len(strLine)
        strCharacter = Mid(strLine, a, Len(strDelimiter))
        Select Case True
            Case strCharacter = strDelimiter
                If Not blnInQualifier Then
                    b = b + 1
                    a = a + (Len(strDelimiter) - 1)
                    If a = Len(strLine) Then
                        If ArrayCount(strHolder) < b Then
                            ReDim Preserve strHolder(b)
                        End If
                    End If
                Else
                    If ArrayCount(strHolder) < b Then
                        ReDim Preserve strHolder(b)
                    End If
                    strHolder(b) = strHolder(b) & Left(strCharacter, 1)
                End If
            Case Left(strCharacter, 1) = Chr(34)
                If blnInQualifier Then
                    If a = Len(strLine) And Mid(strLine, (a - 1), Len(strDelimiter)) = Chr(34) Then
                        ReDim Preserve strHolder(b)
                    End If
                End If
                blnInQualifier = Not blnInQualifier
            Case Else
                If ArrayCount(strHolder) < b Then
                    ReDim Preserve strHolder(b)
                End If
                strHolder(b) = strHolder(b) & Left(strCharacter, 1)
        End Select
    Next a
 
    SplitLine = strHolder
 
    Exit Function
 
SplitLine_Error:
 
    MsgBox Err.Description & " (SplitLine)", vbSystemModal
 
End Function


